package com.feisi.controller;

import com.feisi.domain.User;
import com.feisi.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 查询所有User
     *
     * @return
     */
    @RequestMapping("/findAll")
    public @ResponseBody
    Map<String, Object> findAll(HttpServletRequest request) {
        System.out.println("表现层:查询所有用户");
        Integer page = Integer.valueOf(request.getParameter("page"));
        Integer limit = Integer.valueOf(request.getParameter("limit"));
        List<User> users = userService.findAll((page - 1) * limit, limit);
//        System.out.println("====="+users);
        int count = userService.usersTotal();
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", users);
        return map;
    }

    /**
     * 根据姓名查user
     *
     * @param u_name 从前端输入框获得
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByName")
    public @ResponseBody
    Map<String, Object> findByName(String u_name, @RequestParam("page") Integer page, @RequestParam("limit") Integer limit) {
        List<User> users = userService.findByName(u_name, (page - 1) * limit, limit);
        int count = userService.usersTotalByName(u_name);
        System.out.println("学习maven");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", users);
        return map;
    }

    /**
     * 根据用户(员工)部门查找用户
     * 实现通讯录模板的 选择部门 功能
     * @param u_department
     * @param page
     * @param limit
     * @return
     */
    @RequestMapping("/findByDepartment")
    public @ResponseBody Map<String,Object> findByDepartment(String u_department,@RequestParam("page") Integer page, @RequestParam("limit") Integer limit){
        List<User> users = userService.findByDepartment(u_department, page, limit);
        int count = userService.userTotalByDepartment(u_department);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "");
        map.put("count", count);
        map.put("data", users);
        return map;
    }


    /**
     * 新增user
     *
     * @param
     * @throws IOException
     */
    @RequestMapping("/saveUser")
    public @ResponseBody
    Map<String,Object>  saveUser(String u_name, String u_doc_type, String u_phone, String u_doc_number,String u_email, String u_sex,String userProvinceId,String userCityId ,String userDistrictId, Integer u_age, String u_birthday,String u_ismarry,
          String u_native,String u_health, String u_education,Integer u_job_number,String u_department,String u_entry_time,String u_job,String u_superior) throws ParseException {
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//yyyy-mm-dd, 会出现时间不对, 因为小写的mm是代表: 秒
   /*     Date utilDate = sdf.parse(u_entry_time);
        Date utilDate2=sdf.parse(u_birthday);
        System.out.println(utilDate+"======="+utilDate2);//查看utilDate的值
        Date date = new java.sql.Date(utilDate.getTime());
        Date date2 = new java.sql.Date(utilDate2.getTime());*/

        User user=new User();
        user.setU_name(u_name);
        user.setU_doc_type(u_doc_type);
        user.setU_doc_number(u_doc_number);
        user.setU_phone(u_phone);
        user.setU_sex(u_sex);
        user.setU_age(u_age);
        user.setU_birthday(u_birthday);
        user.setU_ismarry(u_ismarry);
        user.setU_native(userProvinceId+"-"+userCityId+"-"+userDistrictId);
        user.setU_health(u_health);
        user.setU_education(u_education);
        user.setU_job_number(u_job_number);
        user.setU_department(u_department);
        user.setU_entry_time( u_entry_time);
        user.setU_job(u_job);
        user.setU_superior(u_superior);
        user.setU_email(u_email);
        System.out.println("user="+user);
        userService.saveUser(user);
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 修改user
     *
     * @param
     */
    @RequestMapping("/updateUser")
    public @ResponseBody
    Map<String,Object> updateUser(Integer id, String u_name, String u_doc_type, String u_phone, String u_doc_number, String u_sex,String userProvinceId,String userCityId ,String userDistrictId, Integer u_age, String u_birthday,String u_ismarry,
               String u_health, String u_education,Integer u_job_number,String u_department,String u_entry_time,String u_job,String u_superior, String u_email) {
        User user=new User();
        user.setId(id);
        user.setU_name(u_name);
        user.setU_doc_type(u_doc_type);
        user.setU_doc_number(u_doc_number);
        user.setU_phone(u_phone);
        user.setU_sex(u_sex);
        user.setU_age(u_age);
        user.setU_birthday(u_birthday);
        user.setU_ismarry(u_ismarry);
        user.setU_native(userProvinceId+"-"+userCityId+"-"+userDistrictId);
        user.setU_health(u_health);
        user.setU_education(u_education);
        user.setU_job_number(u_job_number);
        user.setU_department(u_department);
        user.setU_entry_time( u_entry_time);
        user.setU_job(u_job);
        user.setU_email(u_email);
        user.setU_superior(u_superior);
        System.out.println("user="+user);
        userService.updateUser(user);
        System.out.println("修改用户");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }


    /**
     * 根据id删除用户
     * 批量删除
     *
     * @param ids
     *
     */
    @RequestMapping(value = "/deleteUser",method = {RequestMethod.POST})
    public @ResponseBody
    Map<String, Object> deleteCustomer(@RequestBody String[] ids) {
        Integer[] id = new Integer[ids.length];
        System.out.println(ids.length);
        System.out.println("ids.length====================="+ids.length);
        for (int i=0;i<ids.length;i++){
            System.out.println(ids[i]);
            Integer idOne = Integer.valueOf(ids[i]);
            id[i]=idOne;

        }
        System.out.println(id+"=========================");
        userService.deleteUser(id);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code", 0);
        map.put("msg", "success");
        map.put("count", 0);
        map.put("data", null);
        return map;
    }

    /**
     * 修改密码
     *
     * @param
     * @param
     */
    @RequestMapping("/updateUserPassword")
    public @ResponseBody
    int updateUserPassword(HttpServletRequest request) {
        Integer userid = (Integer) request.getSession().getAttribute("userid");
        String username = (String) request.getSession().getAttribute("username");
        String oldpassword = request.getParameter("oldpassword");
        String newpassword = request.getParameter("newpassword");
        List<User> users = userService.findByNameAndPassword(username, oldpassword);
        System.out.println(users);
        if (users.size() == 0) {
            return 400;
        } else {
            userService.updateUserPassword(userid, newpassword);
            return 200;
        }
    }


}
